﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.AccountManagement;

namespace ICeFramework.Common.ActiveDirectory
{
    [Serializable]
    public class GroupInfo : ADObject
    {
        private string _emailAddress;
        #region public string EmailAddress
        /// <summary>
        /// Get/Sets the EmailAddress of the GroupInfo
        /// </summary>
        /// <value></value>
        public string EmailAddress
        {
            get { return _emailAddress; }
            set { _emailAddress = value; }
        }
        #endregion

        #region public override ADObjectType ObjectType
        /// <summary>
        /// Gets the ObjectType of the GroupInfo
        /// </summary>
        /// <value></value>
        public override ADObjectType ObjectType
        {
            get { return ADObjectType.Group; }
        }
        #endregion

        #region public GroupInfo()
        /// <summary>
        /// Initializes a new instance of the <b>GroupInfo</b> class.
        /// </summary>
        public GroupInfo()
            : this(string.Empty, ADContextType.Unknown, null)
        {

        }
        #endregion

        #region public GroupInfo(string groupName, ADContextType contextType, GroupPrincipal groupPrincipal)
        /// <summary>
        /// Initializes a new instance of the <b>GroupInfo</b> class.
        /// </summary>
        /// <param name="groupName"></param>
        /// <param name="contextType"></param>
        /// <param name="groupPrincipal"></param>
        public GroupInfo(string groupName, ADContextType contextType, GroupPrincipal groupPrincipal)
            : base(groupName, contextType, groupPrincipal)
        {
        }
        #endregion

        #region public static GroupInfo GetGroupInfo(GroupPrincipal groupPrincipal, ADContextType groupContextType)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupPrincipal"></param>
        /// <param name="groupContextType"></param>
        /// <returns></returns>
        public static GroupInfo GetGroupInfo(GroupPrincipal groupPrincipal, ADContextType groupContextType)
        {
            if (groupPrincipal == null)
            {
                return null;
            }
            GroupInfo gInfo = new GroupInfo(groupPrincipal.Name, groupContextType, groupPrincipal);
            gInfo.DisplayName = string.IsNullOrEmpty(groupPrincipal.DisplayName) ? groupPrincipal.Name : groupPrincipal.DisplayName;

            return gInfo;
        }
        #endregion

        public IList<ADObject> GetMembers()
        {
            return GetMembers(ContextType);
        }

        public IList<ADObject> GetMembers(ADContextType contextType)
        {
            List<ADObject> info = new List<ADObject>();
            if (_objectPrincipal == null)
            {
                return info;
            }

            ContextType groupType = (ContextType)Enum.Parse(typeof(ContextType), contextType.ToString(), true);
            PrincipalContext groupContext = new PrincipalContext(groupType);

            PrincipalSearchResult<Principal> sr = (_objectPrincipal as GroupPrincipal).GetMembers(true);
            foreach (Principal princ in sr)
            {
                if (princ is GroupPrincipal)
                {
                    info.Add(GroupInfo.GetGroupInfo(princ as GroupPrincipal, ContextType));
                }
                if (princ is UserPrincipal)
                {
                    info.Add(UserInfo.GetUserInfo(princ as UserPrincipal, ContextType));
                }
            }

            return info;
        }
    }
}
